package com.bob.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 名称: RedisConfiguration<br>
 * 描述:<br>
 * 类型: JAVA<br>
 * 最近修改时间:2017/7/11 14:01<br>
 *
 * @author BoYangsh
 * @version [版本号, V1.0]
 * @since 2017/7/11 14:01
 */
@Configuration
public class RedisConfiguration {

	@Value("${spring.redis.cluster.nodes}")
	private String nodes;
	@Value("${spring.redis.cluster.max-redirects}")
	private int maxRedirects;
	@Value("${spring.redis.timeout}")
	private int timeout; //超时时间

	@Value("${spring.redis.pool.max-active}")
	private int maxActive;
	@Value("${spring.redis.pool.max-idle}")
	private int maxIdle; //最大空闲数
	@Value("${spring.redis.pool.max-wait}")
	private long maxWait; //最长等待时间
	@Value("${spring.redis.pool.min-idle}")
	private int minIdle; //最小空闲数
	/**
	 * cluster redis的节点配置
	 * @return
	 */
	@Bean
	public RedisClusterConfiguration redisClusterConfiguration() {
		RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
		String[] nodeArr = nodes.split(",");
		for(int i =0; i < nodeArr.length; i++) {
			String[] ipPort = nodeArr[i].split(":");
			RedisNode redisNode = new RedisNode(ipPort[0].trim(), Integer.parseInt(ipPort[1].trim()));
			redisClusterConfiguration.addClusterNode(redisNode);
		}
		return redisClusterConfiguration;
	}

	/**
	 * jedis pool配置
	 * @return
	 */
	@Bean
	public JedisPoolConfig jedisPoolConfig() {
		JedisPoolConfig poolConfig = new JedisPoolConfig();
		poolConfig.setMaxIdle(maxIdle);
		poolConfig.setMinIdle(minIdle);
		poolConfig.setMaxWaitMillis(maxWait);
		poolConfig.setMaxTotal(maxActive);
		return poolConfig;
	}

	/**
	 * jedis工厂
	 * @param redisClusterConfiguration
	 * @param jedisPoolConfig
	 * @return
	 */
	@Bean
	public JedisConnectionFactory jedisConnectionFactory(RedisClusterConfiguration redisClusterConfiguration, JedisPoolConfig jedisPoolConfig) {
		JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration);
		jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
		return jedisConnectionFactory;
	}

	@Bean
	public RedisTemplate<String, String> redisTemplate(JedisConnectionFactory jedisConnectionFactory) {
		ObjectMapper mapper = new ObjectMapper();
		mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
		mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

		StringRedisTemplate template = new StringRedisTemplate(jedisConnectionFactory);
		GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(mapper);
		template.setValueSerializer(genericJackson2JsonRedisSerializer);
		template.afterPropertiesSet();
		return template;
	}


}
